﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Mytrip.Files
{
    public class FileRepository
    {
        static string dir = HttpContext.Current.Server.MapPath("/");
        DirectoryInfo _df = new DirectoryInfo(dir);
        /// <summary>
        /// Получить все папки из под дирректории 
        /// </summary>
        /// <returns></returns>
        public DirectoryInfo[] Get_folders(string directory)
        {
            directory = directory.Replace("()", "/");
            string absolutDirectory = HttpContext.Current.Server.MapPath(directory);
            DirectoryInfo _absolutDirectory = new DirectoryInfo(absolutDirectory);
            if (!_absolutDirectory.Exists)
            {
                _absolutDirectory.Create();
            }
            DirectoryInfo[] result = _absolutDirectory.GetDirectories();
            return result;
        }
        public DirectoryInfo[] Get_folders()
        {
            string absolutDirectory = HttpContext.Current.Server.MapPath("/");
            DirectoryInfo _absolutDirectory = new DirectoryInfo(absolutDirectory);
            if (!_absolutDirectory.Exists)
            {
                _absolutDirectory.Create();
            }
            DirectoryInfo[] result = _absolutDirectory.GetDirectories();
            return result;
        }
        /// <summary>
        /// Получить все файлы из под дирректории 
        /// </summary>
        /// <returns></returns>
        public FileInfo[] Get_files_folder(string directory)
        {
            directory = directory.Replace("()", "/");
            string absolutDirectory = HttpContext.Current.Server.MapPath(directory);
            DirectoryInfo _absolutDirectory = new DirectoryInfo(absolutDirectory);
            FileInfo[] result = _absolutDirectory.GetFiles();
            return result;
        }
        public FileInfo[] Get_files_folder()
        {
            string absolutDirectory = HttpContext.Current.Server.MapPath("/");
            DirectoryInfo _absolutDirectory = new DirectoryInfo(absolutDirectory);
            FileInfo[] result = _absolutDirectory.GetFiles();
            return result;
        }
        /// <summary>
        /// Загрузить файл
        /// </summary>
        /// <returns></returns>
        public void Upload_file(string directory, HttpPostedFileBase file)
        {
            //string c = a;
            if (directory == "")
                directory = "/";
            else
                directory = directory.Replace("()", "/");
            string absolutDirectory = HttpContext.Current.Server.MapPath(directory);
            string name = Path.GetFileName(file.FileName);
            name = DecodingString(name);
            string _absolutDirectory = Path.Combine(absolutDirectory, name);
            file.SaveAs(_absolutDirectory);
        }
        /// <summary>
        /// Удалить файл
        /// </summary>
        /// <returns></returns>
        public void Delete_file(string directory)
        {
            string _directory = directory;
            _directory = _directory.Replace("()", "/");
            string absolutDirectory = HttpContext.Current.Server.MapPath(_directory);
            FileInfo file = new FileInfo(absolutDirectory);
            file.Delete();

        }
        /// <summary>
        /// Создать папку
        /// </summary>
        /// <returns></returns>
        public void Create_folder(string directory)
        {
            string _directory = DecodingString(directory);
            _directory = _directory.Replace("()", "/");
            string absolutDirectory = HttpContext.Current.Server.MapPath(_directory);
            DirectoryInfo folder = new DirectoryInfo(absolutDirectory);
            folder.Create();

        }
        /// <summary>
        /// Удалить папку
        /// </summary>
        /// <returns></returns>
        public void Delete_folder(string directory)
        {
            directory = directory.Replace("()", "/");
            string _directory = HttpContext.Current.Server.MapPath(directory);
            DirectoryInfo folder = new DirectoryInfo(_directory);
            folder.Delete(true);

        }
        /// <summary>
        /// Переименовать папку
        /// </summary>
        /// <returns></returns>
        public void Rename_folder(string a, string c)
        {
            string of = c.Replace("()", "/");
            string nf = c.Remove(c.LastIndexOf("()")).Replace("()", "/") + "/" + DecodingString(a);

            string oldName = HttpContext.Current.Server.MapPath(of);
            string newName = HttpContext.Current.Server.MapPath(nf);
            if (oldName != newName)
                Directory.Move(oldName, newName);
        }
        /// <summary>
        /// Переименовать файл
        /// </summary>
        /// <returns></returns>
        public void Rename_file(string a, string c)
        {
            string of = c.Replace("()", "/");
            string nf = c.Remove(c.LastIndexOf("()")).Replace("()", "/") + "/" + DecodingString(a);

            string oldName = HttpContext.Current.Server.MapPath(of);
            string newName = HttpContext.Current.Server.MapPath(nf);
            if (oldName != newName)
                File.Move(oldName, newName);
        }
        #region Перевод из русского на латиницу
        /// <summary>
        /// Перевод из русского на латиницу
        /// </summary>
        /// <param name="a">строка</param>
        /// <returns></returns>
        public string DecodingString(string a)
        {
            a = a.Trim();
            a = a.Replace(" ", "_");
            a = a.Replace("А", "A");
            a = a.Replace("а", "a");
            a = a.Replace("Б", "B");
            a = a.Replace("б", "b");
            a = a.Replace("В", "V");
            a = a.Replace("в", "v");
            a = a.Replace("Г", "G");
            a = a.Replace("г", "g");
            a = a.Replace("Д", "D");
            a = a.Replace("д", "d");
            a = a.Replace("Е", "E");
            a = a.Replace("е", "e");
            a = a.Replace("Ё", "E");
            a = a.Replace("ё", "e");
            a = a.Replace("Ж", "J");
            a = a.Replace("ж", "j");
            a = a.Replace("З", "Z");
            a = a.Replace("з", "z");
            a = a.Replace("И", "I");
            a = a.Replace("и", "i");
            a = a.Replace("Й", "Y");
            a = a.Replace("й", "y");
            a = a.Replace("К", "K");
            a = a.Replace("к", "k");
            a = a.Replace("Л", "L");
            a = a.Replace("л", "l");
            a = a.Replace("М", "M");
            a = a.Replace("м", "m");
            a = a.Replace("Н", "N");
            a = a.Replace("н", "n");
            a = a.Replace("О", "O");
            a = a.Replace("о", "o");
            a = a.Replace("П", "P");
            a = a.Replace("п", "p");
            a = a.Replace("Р", "R");
            a = a.Replace("р", "r");
            a = a.Replace("С", "S");
            a = a.Replace("с", "s");
            a = a.Replace("Т", "T");
            a = a.Replace("т", "t");
            a = a.Replace("У", "U");
            a = a.Replace("у", "u");
            a = a.Replace("Ф", "F");
            a = a.Replace("ф", "f");
            a = a.Replace("Х", "H");
            a = a.Replace("х", "h");
            a = a.Replace("Ц", "C");
            a = a.Replace("ц", "c");
            a = a.Replace("Ч", "Ch");
            a = a.Replace("ч", "ch");
            a = a.Replace("Ш", "Sh");
            a = a.Replace("ш", "sh");
            a = a.Replace("Щ", "Sh");
            a = a.Replace("щ", "sh");
            a = a.Replace("Ъ", "");
            a = a.Replace("ъ", "");
            a = a.Replace("Ы", "Y");
            a = a.Replace("ы", "y");
            a = a.Replace("Ь", "");
            a = a.Replace("ь", "");
            a = a.Replace("Э", "E");
            a = a.Replace("э", "e");
            a = a.Replace("Ю", "Yu");
            a = a.Replace("ю", "yu");
            a = a.Replace("Я", "Ya");
            a = a.Replace("я", "ya");
            a = a.Replace("!", "");
            a = a.Replace(",", "");


            return a;

        }
        #endregion
    }
}
